﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Xml;

namespace DataAccessor.Tools
{
    public delegate void DatabaseChangedEventHandler(UCDatabase sender);
    public partial class UCDatabase : UserControl
    {
        public UCDatabase()
        {
            InitializeComponent();
            SetTabIndex();
        }

        #region events
        public event DatabaseChangedEventHandler DatabaseChanged;
        private void OnDatabaseChanged(UCDatabase sender)
        {
            if (DatabaseChanged != null)
            {
                DatabaseChanged(sender);
            }
        }
        #endregion

        private bool _miniMode = false;
        public bool MiniMode
        {
            get { return _miniMode; }
            set
            {
                _miniMode = value;
                if (_miniMode)
                {
                    this.labelTitle.Text = string.Format("Database:{0}", this.txtName.Text);
                    this.picExpend.Image = global::DataAccessor.Tools.Properties.Resources._112_DownArrowLong_Blue_16x16_72;
                    this.panelEditor.Hide();
                    this.Size = new Size(this.Width, 30);
                }
                else
                {
                    this.labelTitle.Text = "Database";
                    this.picExpend.Image = global::DataAccessor.Tools.Properties.Resources._112_UpArrowLong_Grey_16x16_72;
                    this.Size = new Size(this.Width, 185);
                    this.panelEditor.Show();
                }
            }
        }

        private HolisticDatabase _database;
        public HolisticDatabase Database
        {
            get
            {
                if (_database == null)
                {
                    _database = new HolisticDatabase();
                    _database.Config = new HolisticDatabaseConfig[] { };
                }
                _database.Name = this.txtName.Text;
                _database.ConnectionString = this.txtConnectionString.Text;
                _database.DataAccessor = this.cmbImplement.Text;
                _database.Protector = this.cmbProtector.Text;
                return _database;
            }
            set
            {
                _database = value;
                if (_database != null)
                {
                    this.txtName.Text = _database.Name;
                    this.txtConnectionString.Text = _database.ConnectionString;
                    this.cmbImplement.Text = _database.DataAccessor;
                    this.cmbProtector.Text = _database.Protector;
                }
            }
        }

        private SkinEnum _skin = SkinEnum.Normal;
        public SkinEnum Skin
        {
            set
            {
                _skin = value;
                switch (_skin)
                {
                    case SkinEnum.Focused:
                        this.panelTitle.BackgroundImage = global::DataAccessor.Tools.Properties.Resources.bgtitle_blue;
                        break;
                    case SkinEnum.Error:
                        this.panelTitle.BackgroundImage = global::DataAccessor.Tools.Properties.Resources.bgtitle_orange;
                        break;
                    case SkinEnum.Normal:
                    default:
                        this.panelTitle.BackgroundImage = global::DataAccessor.Tools.Properties.Resources.bgtitle_gray;
                        break;
                }
            }
        }

        public override ContextMenuStrip ContextMenuStrip
        {
            get
            {
                return base.ContextMenuStrip;
            }
            set
            {
                this.panelTitle.ContextMenuStrip = value;
                // base.ContextMenuStrip = value;
            }
        }

        public bool ValidateSetting()
        {
            var result = true;
            if (string.IsNullOrWhiteSpace(this.txtName.Text))
            {
                result = false;
                errorProvider.SetError(txtName, "请设置Database的Name.");
            }
            else
            {
                errorProvider.SetError(txtName, "");
            }

            if (string.IsNullOrWhiteSpace(this.txtConnectionString.Text))
            {
                result = false;
                errorProvider.SetError(txtConnectionString, "请设置Database的ConnectionString.");
            }
            else
            {
                errorProvider.SetError(txtConnectionString, "");
            }

            if (string.IsNullOrWhiteSpace(this.cmbImplement.Text))
            {
                result = false;
                errorProvider.SetError(cmbImplement, "请设置Database的IDataAccessor的实现.");
            }
            else
            {
                errorProvider.SetError(cmbImplement, "");
            }
            return result;
        }

        private void picExpend_Click(object sender, EventArgs e)
        {
            MiniMode = !MiniMode;
        }

        private void SetTabIndex()
        {
            this.txtName.TabIndex = this.TabIndex + 1;
            this.txtConnectionString.TabIndex = this.TabIndex + 2;
            this.cmbImplement.TabIndex = this.TabIndex + 3;
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            OnDatabaseChanged(this);
        }

        private void UCDatabaseSetting_Click(object sender, EventArgs e)
        {
            this.Focus();
            this.Skin = SkinEnum.Focused;
        }
    }
}
